<!--
 * @Author: your name
 * @Date: 2021-12-03 13:39:36
 * @LastEditTime: 2021-12-03 14:00:16
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \协同编辑\About.vue
-->
<template>
  <div>
    <v-md-editor v-model="text" height="500px"></v-md-editor>
  </div>
</template>
<script>
export default {
  data() {
    return {
      text: "",
      ws: null,
    };
  },
  mounted() {
    if (window.WebSocket) {
      //实例化出来对象
      this.ws = new WebSocket("ws://localhost:8181");
    }
    this.ws.onopen = (e) => {
      console.log("链接成功");
      this.ws.send(
        JSON.stringify({
          chat: this.text,
        })
      );
    };
    this.ws.onmessage = (e) => {
      let resData = JSON.parse(e.data);
      console.log(resData);
      this.text = resData.chat;
    };
    this.ws.onerror = () => {
      console.log("出错了");
    };
  },
  watch: {
    text(newval, oldval) {
      if (newval !== oldval) {
        this.ws.send(
          JSON.stringify({
            chat: newval,
          })
        );
      }
    },
  },
};
</script>
